<#include "/common/_header.html" />
<style type="text/css">
	.infinite-scroll-preloader {margin-top:-2rem;}
	.floatBtn { background: #f16522;display: block;  position: fixed;  right: 15px;  bottom: 55px;  width: 45px;  height: 45px; border: 1px solid #f16522;  box-sizing: border-box;  -webkit-box-sizing: border-box;  border-radius: 50%;  -webkit-border-radius: 50%;  -moz-border-radius: 50%;  font-size: 12px;  color: #999999;  z-index: 99;  text-align: center; }
	.floatBtn>p{ margin-top: -5px;color:#fff;}
	.floatBtn>i{margin-top: 1rem;color:#fff;font-size:1.5rem;}
	.floatBtn:active{ background:#f16522; color: #fff; border: 1px solid #fff;}	
</style>
<div class="page-group"><!-- 页面组 -->
	<div class="page page-current"><!-- 页面组中的每一个页面，可以互相跳转 -->
		<header class="bar bar-nav"><!-- 每个页面的头部 -->
			<#if uservo??>
				<h1 class="title" style="font-size: 1.1rem;font-weight:bold;">今日特价<span style="color:#f16522">*${.now?string("MM·dd")}*</span></h1>
			<#else>
				<!-- <a class="button button-link button-nav pull-left" onclick="refreshBbs()">
					<span class="iconfont icon-refresh" style="font-size: 1.5rem;"></span>
				</a> -->
				<h1 class="title" style="font-size: 1rem;">${sellervo.sellerName}-管理</h1>
			</#if>
		</header>
		<#if sellervo??>
		<nav class="bar bar-tab">
		    <a ${(status?? && status = "0")?string('class="tab-item external active"','class="tab-item external"')} href="${path}/discounts?status=0">
		        <span class="iconfont icon-new" style="display:block;font-size:1.3rem;margin-bottom: -0.5rem;"></span>
		        <span class="tab-label" style="font-size:0.5rem;">发布中</span>
		    </a>
		    <a ${(status?? && status = "1")?string('class="tab-item external active"','class="tab-item external"')} href="${path}/discounts?status=1">
		        <span class="iconfont icon-countdown" style="display:block;font-size:1.3rem;margin-bottom: -0.5rem;"></span>
		        <span class="tab-label" style="font-size:0.5rem;">待发布</span>
		    </a>
		    <a ${(status?? && status = "2")?string('class="tab-item external active"','class="tab-item external"')} href="${path}/discounts?status=2">
		        <span class="iconfont icon-close" style="display:block;font-size:1.3rem;margin-bottom: -0.5rem;"></span>
		        <span class="tab-label" style="font-size:0.5rem;">已下架</span>
		    </a>
		    <a ${(status?? && status = "9")?string('class="tab-item external active"','class="tab-item external"')} href="${path}/discounts/configs?status=9">
		        <span class="iconfont icon-settings" style="display:block;font-size:1.3rem;margin-bottom: -0.5rem;"></span>
		        <span class="tab-label" style="font-size:0.5rem;">我的</span>
		    </a>
		</nav>
		</#if>
		<input type="hidden" name="status" id="status" value="${status!0}">
		<div class="content infinite-scroll infinite-scroll-bottom" data-distance="100"><!-- 每个页面的内容区域 -->
			<div class="card-list"></div>
			<div class="infinite-scroll-preloader">
              <div class="preloader"></div>
            </div>
		</div>
		<#if sellervo??>
			<a href="javascript:void(0)" class="floatBtn" onclick="addDiscount()">
			    <i class="iconfont icon-add"></i>
			</a>
		</#if>
	</div>
</div>
<#include "/common/_footer.html" />
<script src="${path}/resource/js/dateUtil.js"></script>
<script src="${path}/resource/js/layer/layer.js"></script>
<script>
//加载flag
var loading = false;
$('.infinite-scroll-preloader').hide();
// 最多可加载的条目
var maxItems = 50;
//预先加载10条
$.showIndicator();
//加载第一批帖子
$(".infinite-scroll-bottom .card-list").empty().load("${path}/discounts/list?status="+$('#status').val(),function(){
	$.hideIndicator();
	parsePrettyDate();
});
// 上次加载的序号
var lastIndex = 10;
// 注册'infinite'事件处理函数
$(document).on('infinite', '.infinite-scroll-bottom',function() {
    // 如果正在加载，则退出
    if (loading) return;
    // 设置flag
    loading = true;
    if (lastIndex >= maxItems) {
        // 加载完毕，则注销无限加载事件，以防不必要的加载
        $.detachInfiniteScroll($('.infinite-scroll'));
        // 删除加载提示符
        $('.infinite-scroll-preloader').remove();
        //$.toast("没有更多内容了");
        return;
    }
    $('.infinite-scroll-preloader').show();
    var currentPage = (parseInt(lastIndex)/10)+1;
    $.ajax({
		type:'post',
		url:'${path}/discounts/list',
		data:{'currentPage':currentPage,'status':$('#status').val()},
		success:function(data){
			maxItems = $($("input[name='_totalRow']")[0]).val();
	        // 添加新条目
	        $('.infinite-scroll-bottom .card-list').append(data);
	        // 更新最后加载的序号
	        lastIndex = $('.card-list .card').length;
	        //容器发生改变,如果是js滚动，需要刷新滚动
	        $.refreshScroller();
	     	// 重置加载flag
	        loading = false;
	        parsePrettyDate();
	    },
		error:function(xhr,errorType , error) {
			console.log("sorry error\r\n");
		}
    });
});
function addDiscount(){
	location.href = "${path}/discounts/add";
}
function showpic(imgsrc,bbsid,obj){
	layer.open({
	  type: 1
	  ,content: '<div onclick="javascript:layer.closeAll();" style="width:100%; height:100%;"><img src="'+imgsrc+'" style="max-width:100%;height:auto;"></div>'
	  ,style: 'background-color:#000000;display:flex;justify-content:center;align-items:center;position:fixed;left:0;top:0;width:100%; height:100%; border: none; -webkit-animation-duration: .5s; animation-duration: .5s;'
	});
    <#if uservo??>
	    //add active count
	    $.ajax({
			type:'post',
			url:'${path}/communityChat/clickPicture',
			data:{"bbsid":bbsid},
			success:function(data){
				if(data=="OK"){
					var cucount = parseInt($(obj).parent().parent().children(".row").children(".activeCountA").children(".activeCount").text());
					if(cucount==0){
						$(obj).parent().parent().children(".row").children(".activeCountA").children(".activeCount").prev().removeClass("iconfont icon-attention");
						$(obj).parent().parent().children(".row").children(".activeCountA").children(".activeCount").prev().addClass("iconfont icon-attentionfill");
					}
					$(obj).parent().parent().children(".row").children(".activeCountA").children(".activeCount").text(cucount+1);
				}else{
					console.log("Error.");
				}
			},
			error:function(xhr,errorType , error) {
				console.log("sorry error\r\n");
			}
		});
    </#if>
}
<#if uservo??>
	function sendyes(obj,bbsid){
		var yes = 1;
		if($(obj).children("i").attr('class')=='iconfont icon-like'){
			$(obj).children("i").removeClass("iconfont icon-like");
			$(obj).children("i").addClass("iconfont icon-likefill");
		}else{
			return;
		}
		$.showIndicator();
		$.ajax({
			type:'post',
			url:'${path}/communityChat/addYes',
			data:{"sayyes.yes_targetid":bbsid,"sayyes.yes_target_type":1,"sayyes.sayyes":yes},
			success:function(data){
				var cucount = parseInt($(obj).children(".yescount").text());
				if(yes){
					$(obj).children(".yescount").text(cucount+1);
					var account = parseInt($(obj).parent().children(".activeCountA").children(".activeCount").text());
					if(account==0){
						$(obj).parent().children(".activeCountA").children(".activeCount").prev().removeClass("iconfont icon-attention");
						$(obj).parent().children(".activeCountA").children(".activeCount").prev().addClass("iconfont icon-attentionfill");
					}
					$(obj).parent().children(".activeCountA").children(".activeCount").text(account+1);
					var footObj = $(obj).parent().parent().next();
					if(cucount==0){
						var htmlStr = '<div class="yeslist"><span class="iconfont icon-likefill" style="color:#f16522;margin-right:0.2rem;"></span>'+
						'<span class="yesNameStr">${uservo.nickname}</span></div><hr style="border-color: rgba(204, 204, 204, 0.2);">';
						footObj.children().first().before(htmlStr);
					}else{
						var yesListObj = footObj.find(".yesNameStr");
						yesListObj.text(yesListObj.text()+", ${uservo.nickname}");
					}
					//$(".yeslist").html($(".yeslist").html()+", ${uservo.nickname}");
				}else{
					//$(obj).children(".yescount").text(cucount-1);
				}
				$.hideIndicator();
			},
			error:function(xhr,errorType , error) {
				console.log("sorry error\r\n");
			}
		});
	}
	function sendreply(obj,bbsid){
		var reply = $(obj).prev().val();
		if(reply.trim()==''){
			$.toast("请填写评论内容");
			return ;
		}
		var replylist = $(obj).parent().prev().children();
		$.showIndicator();
		$.ajax({
			type:'post',
			url:'${path}/communityChat/addComment',
			data:{"comments.bbsid":bbsid,"comments.comment":reply},
			success:function(data){
				if(replylist.length>0){
					if(replylist.length>=5){
						replylist[replylist.length-1].remove();
					}
					replylist.first().before("<div>${uservo.nickname}: "+reply+"</div>");
				}else{
					$(obj).parent().prev().append("<div>${uservo.nickname}: "+reply+"</div>");
				}
				$(obj).prev().val("");
				var cucount = parseInt($(obj).parent().parent().prev().find(".comcount").text());
				if(cucount==0){
					$(obj).parent().parent().prev().find(".comcount").prev().removeClass("iconfont icon-comment");
					$(obj).parent().parent().prev().find(".comcount").prev().addClass("iconfont icon-commentfill");
				}
				$(obj).parent().parent().prev().find(".comcount").text(cucount+1);
				cucount = parseInt($(obj).parent().parent().prev().find(".activeCount").text());
				if(cucount==0){
					$(obj).parent().parent().prev().find(".activeCount").prev().removeClass("iconfont icon-attention");
					$(obj).parent().parent().prev().find(".activeCount").prev().addClass("iconfont icon-attentionfill");
				}
				$(obj).parent().parent().prev().find(".activeCount").text(cucount+1);
				console.log("ajax success:"+data);
				$.hideIndicator();
			},
			error:function(xhr,errorType , error) {
				console.log("sorry error\r\n");
			}
		});
	}
</#if>
<#if sellervo??>
	function editBbs(bbsid){
		//location.href = "${path}/discounts/edit?bbsid="+bbsid;
	}
	function deleteBbs(bbsid){
		$.confirm('确认删除', function () {
			$.showIndicator();
			location.href = "${path}/discounts/deleteEntity?bbsid="+bbsid;
	     });
	}
	function downBbs(bbsid){
		$.confirm('确认下架', function () {
			$.showIndicator();
			location.href = "${path}/discounts/downBbs?bbsid="+bbsid;
	     });
	}
	function extendBbs(disid,bbsid){
	    $.modal({
	      title:  '请为该优惠选择延长天数',
	      text: '',
	      verticalButtons: true,
	      buttons: [
	        {
	          text: '延长一天',
	          onClick: function() {
	        	  $.showIndicator();
	        	  location.href = "${path}/discounts/extendBbs?days=1&disid="+disid;
	          }
	        },
	        {
	          text: '延长两天',
	          onClick: function() {
	        	  $.showIndicator();
	        	  location.href = "${path}/discounts/extendBbs?days=2&disid="+disid;
	          }
	        },
	        {
	          text: '延长三天',
	          onClick: function() {
	        	  $.showIndicator();
	        	  location.href = "${path}/discounts/extendBbs?days=3&disid="+disid;
	          }
	        },
	        {
	          text: '取消',
	          close:true,
	          onClick: function() {
	        	  return;
	          }
	        },
	      ]
	    });
	}
</#if>
/*
function refreshBbs(){
	location.href = "${path}/discounts?status="+$("#status").val();
}
*/
</script>